<template>
	<view class="container bar">
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{data.title}}</block>
		</cu-custom>
		<view class="lessons" v-if="data.lessons.length>0">
			<lessons :ismanager="true" :lessons="data.lessons"></lessons>
			<uni-load-more class="bg-white" :status="loadmore" :contentText="contentText"></uni-load-more>
		</view>
		<view v-else class="text-center text-empty">
			<text>空空如也</text>
		</view>
		<view class="cu-bar tabbar">
			<view class="action">
				<button @click="naviTo('lesson/post')" class="cu-btn lg round margin-bottom-xl bg-orange-red shadow">创建新课程</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import core from "@/static/core.js"
	import lessons from "@/components/util/lessons.vue"
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	
	export default {
		computed: mapState(['hasLogin', 'lectorid']),
		components: {uniLoadMore, lessons},
		data() {
			return {
				page:1,
				loading:false,
				contentText:{
					"contentdown":"上滑加载更多数据",
					"contentrefresh":"加载中...",
					"contentnomore":"就这么多了"
				},
				loadmore:'more',
				data:{
					title:"课程管理",
					lessons:[]
				}
			}
		},
		onLoad() {
		},
		onShow() {
			if(!this.hasLogin) return core.toast('请先登录ss','login');
			/**
			 * 讲师认证 限制
			 */
			 if(!this.lectorid) return core.navito('lector/apply',{},1);
			this.loadLessons(1);
		},
		onReachBottom() {
			if(this.loadmore=='noMore') return false;
			return this.loadLessons(this.page+1);
		},
		methods:{
			initData(route, data={}, callback=false){
				var that = this;
				core.get(route,function(res){
					/**
					 * 课程页面 登录限制
					 */
					// if(typeof(res.message)!='undefined' && typeof(res.type)!='undefined'){
					// 	return core.report(res, true);
					// }
					that.data = res;
					uni.setNavigationBarTitle({
						title:res.title
					});
					if(callback){
						callback(res);
					}
				},data);
			},
			naviTo(page,data={}){
				return core.navito(page,data);
			},
			async loadLessons(page=1){
				if(this.loading) return false;
				this.loading = true;
				var that = this;
				(await this.$api.reqGet({
					url:"menu/lessonsByUid/"+core.userinfo.uid
				}).then(res=>{
					console.log(res)
					if(res.code==200){
						that.loading = false;
						that.loadmore = res.data.length<15 ? 'noMore' : 'more';
						that.page = page;
						if(page==1){
							that.data.lessons = res.data;
							// uni.setNavigationBarTitle({
							// 	title:res.title
							// });
						}else{
							that.data.lessons = that.data.lessons.concat(res.data);
						}
					}
				}))
				// core.get('lector/lesson',function(res){
				// 	that.loading = false;
				// 	/**
				// 	 * 课程页面 登录限制
				// 	 */
				// 	// if(typeof(res.message)!='undefined' && typeof(res.type)!='undefined'){
				// 	// 	return core.report(res, page==1?true:false);
				// 	// }
				// 	that.loadmore = res.lessons.length<15 ? 'noMore' : 'more';
				// 	that.page = page;
				// 	if(page==1){
				// 		that.data = res;
				// 		uni.setNavigationBarTitle({
				// 			title:res.title
				// 		});
				// 	}else{
				// 		that.data.lessons = that.data.lessons.concat(res.lessons);
				// 	}
				// },{page:page})
			},
			unSupport(){
				return core.toast('敬请期待');
			}
		}
	}
</script>

<style>
	page{background: #FFFFFF;}
</style>
